#include "MessageFactory.h"
#include "Message.h"

namespace miniproto
{

MessageFactory::MessageFactory()
{
	msg_map_ = new MsgTypeMap();
}

MessageFactory::~MessageFactory()
{
	if (msg_map_ != NULL)
	{
		delete this->msg_map_;
		this->msg_map_ = NULL;
	}
}

void MessageFactory::init()
{
	this->init_msg_map();
}

void MessageFactory::register_message(const ::std::string &type_name, ::miniproto::Message *obj)
{
	(*(this->msg_map_))[type_name] = obj;
}
::miniproto::Message *MessageFactory::create(const ::std::string &type_name) const
{
	MsgTypeMap::const_iterator iter = this->msg_map_->find(type_name);
	if (iter != this->msg_map_->end())
		return iter->second->New();
	return NULL;
}

} // namespace miniproto
